#include"threadPool.h"
#include "threadPool.h"
//服务器下载到本地   服务端负责发送
int recvn(int sockfd, void *buf, int length){
    int total = 0; //已经收到的总长度
    char *p = (char *)buf;
    while(total < length){
        ssize_t sret = recv(sockfd,p+total,length-total,0);
        total += sret;
    }
    return 0;
}
int trainSend(int netfd, const char* pathName, const char* pFileName){
    
    train_t train;
    train.length = strlen(pFileName);
    memcpy(train.data,pFileName,train.length);
    send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
    char buf[4096] = {0};
    sprintf(buf, "%s/%s", pathName, pFileName);
    printf("buf = %s, strlen = %ld\n", buf, strlen(buf));
    int fd = open(buf,O_RDWR|O_CREAT, 0666);
    ERROR_CHECK(fd,-1,"open");
    struct stat statbuf;
    fstat(fd,&statbuf);
    train.length = sizeof(statbuf.st_size);
    memcpy(train.data,&statbuf.st_size,train.length);
    send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
    sendfile(netfd,fd,NULL,statbuf.st_size);
    close(fd);
    return 0;
}
